package cn.wolfcode.service.impl;

import cn.wolfcode.domain.TravelComment;
import cn.wolfcode.query.TravelCommentQuery;
import cn.wolfcode.repository.TravelCommentRepository;
import cn.wolfcode.service.ITravelCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentRepository travelCommentRepository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);
        travelComment.setCreateTime(new Date());

        //维护关联的评论
        String refId = travelComment.getRefComment().getId();
        if(StringUtils.hasLength(refId)){
            TravelComment refComment = this.get(refId);
            //设置关联的评论
            travelComment.setRefComment(refComment);
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);

        }

        travelCommentRepository.save(travelComment);
    }

    @Override
    public void update(TravelComment travelComment) {
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        travelCommentRepository.deleteById(id);
    }

    @Override
    public TravelComment get(String id) {
        return travelCommentRepository.findById(id).orElse(null);

    }

    @Override
    public List<TravelComment> findAll() {
        return travelCommentRepository.findAll();
    }

    @Override
    public Page<TravelComment> query(TravelCommentQuery qo) {
        //travelCommentRepository 查询只能对所有数据进行分页
        //template 查询不能直接对数据进行分页 间接分页

        Query query = new Query();

        if(StringUtils.hasLength(qo.getTravelId())){
            query.addCriteria(Criteria.where("TravelId").is(qo.getTravelId()));
        }


        //totalCount 总记录数
        long totalCount = template.count(query, TravelComment.class);
        if(totalCount == 0){
            return  Page.empty();  //空的分页对象
        }

        //分页参数对象, 参数1 当前页  2 每页显示的条数  3 排序规则  4 排序字段
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "createTime");

        query.with(pageRequest);

        List<TravelComment> list = template.find(query, TravelComment.class);

        return new PageImpl<>(list, pageRequest, totalCount);
    }

    @Override
    public void saveOrUpdate(TravelComment travelComment) {
        if(StringUtils.hasLength(travelComment.getId())){
            //有id修改
            //参数个数一致等同于全量更新
            this.update(travelComment);

        }else{
            this.save(travelComment);
        }
    }

    @Override
    public List<TravelComment> queryByTravelId(String travelId) {

        return travelCommentRepository.findByTravelId(travelId);
    }


}
